<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
            list-style-type: none;
        }

        body {
            height: 1200px;
        }





        #background_box{
            display: table;   /*  居中对齐  */
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            z-index: 99;
            background: rgba(0, 0, 0, 0.5);          /*   背景颜色 半透明  */
        }

        #alert {
            width: 300px;
            height: 300px;
            background-color: pink;

            position: absolute;
            z-index: 1000;
        }







    </style>
    <title>Document</title>
</head>

<body>
<span  id="click_me" style="  font-size: 50px;  ">点我弹出模态框</span>


<div id="background_box"></div>     <!--   半透明 背景-->

<div id="alert"></div>    <!--    粉色框      -->


</body>

<script type="text/javascript">


    document.getElementById('background_box').style.display = 'none';
    document.getElementById('alert').style.display = 'none';

    /*           点击文字出现模态框           */
    document.getElementById('click_me').onclick = function () {
        document.getElementById('background_box').style.display = 'block';
        document.getElementById('alert').style.display = 'block';


        //  禁止页面滚动
        document.body.addEventListener('touchmove',bodyScroll,false);
        document.body.style.position = 'fixed';
        document.body.style.width = '100%';
    };


    /*           点击背景关闭模态框           */
    document.getElementById('background_box').onclick = function () {
        document.getElementById('background_box').style.display = 'none';
        document.getElementById('alert').style.display = 'none';

        //  开启页面滚动
        document.body.removeEventListener('touchmove',bodyScroll,false);
        document.body.style.position = '';
        document.body.style.width = '';
    };


    /*       禁止滑动          */
    //  首先，建立一个函数
    function bodyScroll(event){
        event.preventDefault();
    }



</script>
</html>

